import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * @author shangshu
 * 
 */
public class Solution {

    static List<String> orgStrs = new ArrayList<String>();

    static int          nStrs   = 0;

    /**
     * @param args
     * @throws IOException
     * @throws NumberFormatException
     */
    public static void main(String[] args) throws NumberFormatException,
            IOException {

        Solution findString = new Solution();
        findString.findString();
    }

    static class CandidateManager {
        private List<String> suffixes;
        private int          row;
        private int          col;

        CandidateManager(List<String> s, int r, int c) {
            suffixes = s;
            row = r;
            col = c;
        }

        public String getNextCandidate() {
            if (row >= suffixes.size()) {
                return null;
            }
            String str = suffixes.get(row);
            if ((++col) <= str.length()) {
                return str.substring(0, col);
            } else if ((++row) < suffixes.size()) {
                str = suffixes.get(row);
                String previousStr = suffixes.get(row - 1);
                int column = 0;
                while(column < str.length() && column < previousStr.length() && str.charAt(column) == previousStr.charAt(column))
                {
                    ++column;
                }
                col = ++column;
                return str.substring(0, col);
            } else {
                return null;
            }

        }
    }

    public void findString() throws NumberFormatException, IOException {
        int total = 0;

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        List<String> allStrs = new ArrayList<String>();
        List<String> subSuffixes = new ArrayList<String>();
        nStrs = Integer.parseInt(in.readLine());

        while (nStrs-- > 0) {
            String input = in.readLine();
            allStrs.add(input);
            total += input.length() * input.length() / 2;
        }

        for (String s : allStrs) {
            for (int i = 0; i < s.length(); i++) {
                subSuffixes.add(s.substring(i));
            }
        }

        Collections.sort(subSuffixes);
        List<String> uniqSuffix = new ArrayList<String>();
        uniqSuffix.add(subSuffixes.get(0));
        for (int i = 1; i < subSuffixes.size(); i++) {
            if (!subSuffixes.get(i - 1).equals(subSuffixes.get(i))) {
                uniqSuffix.add(subSuffixes.get(i));
            }
        }

        List<Integer> originalPositions = new ArrayList<Integer>();
        Map<Integer, String> result = new HashMap<Integer, String>();

        int nFind = Integer.parseInt(in.readLine());
        for (int i = 0; i < nFind; i++) {
            int cur = Integer.parseInt(in.readLine());
            originalPositions.add(cur);
        }

        List<Integer> positions = new ArrayList<Integer>();
        positions.addAll(originalPositions);
        Collections.sort(positions);
        int current = 1;
        CandidateManager mgr = new CandidateManager(uniqSuffix, 0, 0);
        String candidate = mgr.getNextCandidate();
        for (int cur : positions) {
            if (cur > total) {
                result.put(cur, "INVALID");
                continue;
            }
            while (current < cur) {
                String newCandidate = mgr.getNextCandidate();
                if (newCandidate == null) {
                    candidate = null;
                    break;
                }
                current++;
                candidate = newCandidate;
            }
            if (current == cur && candidate != null) {
                result.put(cur, candidate);
            } else {
                result.put(cur, "INVALID");
            }
        }

        for (int cur : originalPositions) {
            System.out.println(result.get(cur));
        }
    }

}

